var total_lists = "TOTAL_LISTS";
var util = require("./mid_util");
/**
 * ******************************************
 * 1. 所有基于异步的操作都统一使用回调来完成
 * 2. 所有数据缓存到storage中
 * 3. 2021-07-03: [1, 3, 5]
 * 4. total_lists: [{
 *       list_id: 0,
 *       emotion: 6,//情感
 *       work: 8,//工作内容
 *       title: '标题',
 *       tip: '简介',
 *       isReminder: true,//是否开启提醒
 *       reminderTime: 20,//提醒时间
 *       date: 1626175067190,//创建日期
 *       doneDate: 1626175067190,//完成日期
 *       status: 1,// 0-已完成 1-进行中 2-即将进行
 *    }]
 * ******************************************
 */
module.exports = {
  /**
   * 从某个日期删除TODO_ID
   * @param {日期} date
   * @param {todo任务的ID} todo_id
   * @param {回调} callBack
   */
  deleteFromDate(date, todo_id, callBack) {
    util.getStorage(date, {
      success: res => {
        res.splice(res.indexOf(todo_id), 1)
        util.setStorage(date, util.fromJsonToTxt(res), {
          success: () => {
            util.callBack(callBack, "");
          }
        });
      }
    });
  },
  /**
   * 向某个日期加入新的TODO_ID
   * @param {日期} date
   * @param {todo任务的ID} todo_id
   * @param {回调} callBack
   */
  addDayListTodo: function (date, todo_id, callBack) {
    util.getStorage(date, {
      success: res => {
        res.push(todo_id); //增加新的todo_id
        util.setStorage(date, util.fromJsonToTxt(res), {
          success: () => {
            util.callBack(callBack, "");
          }
        });
      }
    });
  },
  /**
   * 更新某个TODO_ID下的内容
   * @param {日期} date
   * @param {todo任务的ID} todo_id
   * @param {回调} callBack
   */
  updateListByID(list_id, list, callBack) {
    this.getTotalLists({
      //先获取到全部的任务清单
      success: res => {
        for (let index = 0; index < res.length; index++) {
          if (res[index].list_id == list_id) {
            //定位到目标，更新对应的List
            res[index] = list;
            break;
          }
        }
        //把更新后的清单存回去
        util.setStorage(total_lists, JSON.stringify(res), {
          success: () => {
            util.callBack(callBack, "");
          }
        });
      }
    });
  },
  /**
   * 获取某个TODO_ID下的内容
   * @param {todo任务的ID} list_id
   * @param {回调} callBack
   */
  getListByID(list_id, callBack) {
    this.getTotalLists({
      success: res => {
        res.forEach(ele => {
          if (ele.list_id == list_id) {
            //定位到目标
            util.callBack(callBack, ele);
          }
        });
      }
    });
  },
  /**
   * 通过日期获取某个TODO_ID下的内容
   * @param {日期}} date
   * @param {回调} callBack
   */
  getListsByDate(date, callBack) {
    this.getTotalLists({
      success: total_lists => {
        //先获取全部的todo_list
        util.getStorage(date, {
          //根据日期获取当天的List_id
          success: res => {
            var tmp = [];
            res.forEach(ele => {
              tmp.push(total_lists[ele]);
            });
            util.callBack(callBack, tmp);
          }
        });
      }
    });
  },
  /**
   * 获取全部List
   * @param {回调} callBack
   */
  getTotalLists(callBack) {
    util.getStorage(total_lists, {
      success: res => {
        util.callBack(callBack, res);
      }
    });
  },
  /**
   * 增加List
   * @param {数据} data
   * @param {回调} callBack
   */
  addLists: function (data, callback) {
    this.getTotalLists({
      success: () => {
        util.getStorage(total_lists, {
          success: res => {
            //动态生成list_id
            data.list_id = res.length;
            res.push(data);
            util.setStorage(total_lists, util.fromJsonToTxt(res));
            util.callBack(callback, data.list_id); //返回list的id
          }
        });
      }
    });
  }
};
